const object = {
	async getAll(fid) {
		const {
			data: res
		} = await uni.$http.get("/object/getDefaultFridgeObjects")
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		res.data.forEach(x => {
			x.imgSrc = uni.$staticResourcePath + x.imgSrc
			x.id = x.typeId + x.location
		})
		uni.$store.commit('object/updateObjList', res.data.reverse())
		uni.$api.message.getAll()
	},

	async getAllCateAndObj() {
		const {
			data: res
		} = await uni.$http.get("/objectType/getAllObjectType?fid=" + uni.$store.state.fridge.default_fridge
			.fid)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		res.data.forEach(x => {
			x.objectTypeList.forEach(y => {
				y.imgSrc = uni.$staticResourcePath + y.imgSrc
			})
		})
		let index = res.data.findIndex(({
			classification
		}) => classification == '自定义')
		if (index == -1)
			res.data.push({
				classification: '自定义',
				objectTypeList: []
			})
		uni.$store.commit('object/updateCateList', res.data)
	},

	async takeItem(obj) {
		const {
			data: res
		} = await uni.$http.post("/object/takeOutObjects", {
			fid: uni.$store.state.fridge.default_fridge
				.fid,
			typeId: obj.typeId,
			num: obj.num,
			location: obj.location
		})
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		uni.$showMsg('取出成功！')
		this.getAll()
	},

	async storeItem(obj) {
		const {
			data: res
		} = await uni.$http.post("/object/storeObjects", obj)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		uni.$showMsg('存入成功！')
		this.getAll()
	},

	async diy(obj) {
		const {
			data: res
		} = await uni.$http.post("/objectType/saveDefinedObjectType", obj)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		res.data.imgSrc = uni.$staticResourcePath + res.data.imgSrc
		res.data.unit = '份'
		uni.$showMsg(res.msg)
		uni.$store.commit('object/addDiyObj', res.data)
	},

	async updatePersonalObjSetting(obj) {
		const {
			data: res
		} = await uni.$http.post("/objectType/SetUserObjTypePreference", {
			otId: obj.otId,
			defaultLocation: obj.location,
			defaultExpirationDate: obj.warrantTime,
		})
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		uni.$showMsg(res.msg)
		this.getAllCateAndObj()
	},

	async updateFridgeObjSetting(obj) {
		const {
			data: res
		} = await uni.$http.post("/objectType/SetFridgeObjTypePreference", {
			fid: obj.fid,
			otId: obj.otId,
			repositoryAlert: obj.repositoryAlert,
			expiredAlert: obj.expiredAlert,
			defaultUnit: obj.unit
		})
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		uni.$showMsg(res.msg)
		this.getAllCateAndObj()
	},

	async deleteDiy(otId, index) {
		const {
			data: res
		} = await uni.$http.delete('/objectType?otId=' + otId + "&fid=" + uni.$store.state.fridge.default_fridge
			.fid)
		if (res.code !== uni.$code.successCode) return uni.$showMsg(res.msg)
		uni.$showMsg(res.msg)
		uni.$store.commit('object/deleteDiyObj', index)
	},

	// async detect(formData, mode) {
	// 	const {
	// 		data: res,
	// 		statusCode: code
	// 	} = await uni.$http.post(mode === 0 ? "/objectDetection/detect" :
	// 		"/objectDetection/detectByShoppingList", formData
	// 		.buffer, formData.contentType)
	// 	if (code != 200) return uni.$showMsg('服务器繁忙')
	// 	if (res.code !== uni.$code.successCode) return uni.$showMsg('识别异常')
	// 	const objArray = []
	// 	res.data.objectInfos.forEach(x => {
	// 		x.object.imgSrc = uni
	// 			.$staticResourcePath +
	// 			x.object.imgSrc
	// 		x.object.location = '1-1'
	// 		x.object.num = x.detectNum
	// 		objArray.push(x.object)
	// 	})
	// 	return objArray
	// }

}
export default object